import cv2
import numpy as np

red = (0, 0, 255)


# ==============================
# 最大外接矩阵
# ==============================
def test1():
    #
    src = cv2.imread('graph.png')

    # 转换颜色空间
    img = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
    # 二值化
    ret, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    # 查找轮廓
    contours, hierarchy = cv2.findContours(img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    # 最大外接矩形, 返回最大外接矩形的参数, (x,y), (w, h)
    x, y, w, h = cv2.boundingRect(contours[1])
    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

    cv2.imshow('img_copy', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
